#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/25 15:41
# @Author  : huidong.bai
# @File    : demo_pyusb.py
# @Software: PyCharm

import win32com.client
import serial.tools.list_ports
import platform


def get_devices_list():
    ADB_PID_VID = 'VID_2207&PID_0006\\\\'
    CH340_PID_VID = 'VID_1A86&PID_7523\\\\'

    devices = []

    wmi = win32com.client.GetObject("winmgmts:")
    for usb in wmi.InstancesOf("win32_usbcontrollerdevice"):
        if ADB_PID_VID in usb.Dependent:
            infos = {'device_type': 'ADB', 'port': usb.Dependent.split(ADB_PID_VID)[1][:-1]}
            devices.append(infos)
        # if CH340_PID_VID in usb.Dependent:
        #     infos = {'device_type': 'COM', 'port': usb.Dependent.split(CH340_PID_VID)[1][:-1]}
        #     devices.append(infos)

    ports = list(serial.tools.list_ports.comports())
    if len(ports) > 0:
        for each_port in ports:
            if "USB-SERIAL" in each_port[1]:
                infos = {'device_type': 'COM', 'port': each_port[0]}
                devices.append(infos)

    return devices


if __name__ == '__main__':
    devices = get_devices_list()
    print(devices)
